﻿
using System;
using System.Collections.Generic;
using System.Linq;

namespace FacebookXmpp.Parse
{
    public class MessageData
    {
    	private string _name;
    	private object _value;
		
		public string Name
		{
			get { return _name; }
			internal set 
			{ 
				_name = value;
				_isEmpty = false;
			}
		}
    	public object Value
    	{
			get { return _value; }
			internal set
			{
				_value = value;
				_isEmpty = false;
			}
    	}

		public bool IsOpen { get; set; }

    	private bool _isEmpty = true;
    	public bool IsEmpty
    	{
			get { return _isEmpty;  }
    	}

        public Dictionary<string, string> Attributes { get; private set; }

		public void AddAttribute(string name, string value)
		{
			Attributes.Add( name, value );
			_isEmpty = false;
		}

		public MessageData()
        {
            Attributes = new Dictionary<string, string>();
        }

    	public bool HasChildren
    	{
    		get
    		{
				if (Value == null)
					return false;

    			return (typeof (List<MessageData>).IsAssignableFrom( Value.GetType() ) ||
    			        typeof (MessageData).IsAssignableFrom( Value.GetType() ));
    		}
    	}

		public IEnumerable<MessageData> GetChildren()
		{
			if (typeof( List<MessageData> ).IsAssignableFrom( Value.GetType() ))
				foreach (MessageData md in ((List<MessageData>)Value))
					yield return md;

			if (typeof( MessageData ).IsAssignableFrom( Value.GetType() ))
				yield return (MessageData) Value;
		}

        public override string ToString()
        {
            string valueAsString;
            if (Value == null)
            {
                valueAsString = "<null>";
            }
            else if (Value.GetType() == typeof(String))
            {
                valueAsString = "\"" + (string)Value + "\"";
            }
            else if (Value.GetType() == typeof(MessageData))
            {
                valueAsString = "{" + Value + "}";
            }
            else if (Value.GetType() == typeof(List<MessageData>))
            {
                valueAsString = "[";
                foreach (MessageData data in ((List<MessageData>)Value))
                    valueAsString += data + " ";
                valueAsString += "]";
            }
            else
            {
                valueAsString = "???";
            }

            return "{" + Name + " : " + valueAsString + "}";
        }

        public object Path(string path)
        {
            if ((Value == null) || (!(typeof(MessageData).IsAssignableFrom(Value.GetType())) && !(typeof(List<MessageData>).IsAssignableFrom(Value.GetType()))))
                return null;

            if (string.IsNullOrEmpty(path))
                return this;

            string part;
            if (path.IndexOf("/") == -1)
            {
                part = path;
                path = null;
            }
            else
            {
                part = path.Substring(0, path.IndexOf("/"));
                path = path.Substring(path.IndexOf("/") + 1);
            }

            if (typeof(MessageData).IsAssignableFrom(Value.GetType()))
            {
                if (string.Compare(((MessageData)Value).Name, part, StringComparison.OrdinalIgnoreCase) == 0)
                    return string.IsNullOrEmpty(path) ? (MessageData)Value : ((MessageData)Value).Path(path);

                return null;
            }


            IEnumerable<MessageData> found = ((List<MessageData>)Value).Where(x => String.Compare(x.Name, part, StringComparison.OrdinalIgnoreCase) == 0);
            if (found.Count() == 0)
                return null;

            if (found.Count() == 1)
                return string.IsNullOrEmpty(path) ? found.First() : found.First().Path(path);

            return found.ToList();
        }
    }
}